import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class DNSClientFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField textField;
	private JButton btnSend;
	private JTextPane textPane;
	private Style redStyle, blackStyle;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// DNSClientFrame frame = new DNSClientFrame();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public DNSClientFrame() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Information",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(6, 42, 438, 230);
		contentPane.add(panel);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane);

		textPane = new JTextPane();
		scrollPane.setViewportView(textPane);
		textPane.setEditable(false);
		redStyle = textPane.addStyle("Red style", null);
		StyleConstants.setForeground(redStyle, Color.RED);
		blackStyle = textPane.addStyle("Black style", null);
		StyleConstants.setForeground(blackStyle, Color.BLACK);

		textField = new JTextField();
		textField.setBounds(76, 6, 248, 28);
		contentPane.add(textField);
		textField.setColumns(10);

		btnSend = new JButton("Send");
		btnSend.setBounds(327, 7, 117, 29);
		contentPane.add(btnSend);
		getRootPane().setDefaultButton(btnSend);

		JLabel lblDomain = new JLabel("Domain:");
		lblDomain.setBounds(17, 12, 61, 16);
		contentPane.add(lblDomain);
	}

	public String getDomain() {
		return textField.getText();
	}

	public void setActionListener(ActionListener listener) {
		btnSend.addActionListener(listener);
	}

	public void setInformation(String s) {
		textPane.setText("");
		StyledDocument doc = textPane.getStyledDocument();
		try {
			doc.insertString(doc.getLength(),
					s, blackStyle);
		} catch (BadLocationException e) {
		}
	}

	public void setError(String s) {
		textPane.setText("");
		StyledDocument doc = textPane.getStyledDocument();
		try {
			doc.insertString(doc.getLength(), s, redStyle);
		} catch (BadLocationException e) {
		}
	}

	public void showMessage(Message m) {
		textPane.setText("");
		StyledDocument doc = textPane.getStyledDocument();
		if (m == null) {
			try {
				doc.insertString(doc.getLength(),
						"Impossibile contattare il server!\n", redStyle);
			} catch (BadLocationException e) {
			}
		} else {
			if (m.isFound()) {
				try {
					doc.insertString(doc.getLength(),
							m.getDomainInfo().toString(), blackStyle);
				} catch (BadLocationException e) {
				}
			} else {
				try {
					doc.insertString(doc.getLength(),
							"Dominio non presente sul server DNS!\n", redStyle);
				} catch (BadLocationException e) {
				}
			}
		}
	}

	public void setSendEnabled(boolean enabled) {
		btnSend.setEnabled(enabled);
	}
}
